package com.googlecode.curvedmetromaps.core.logic.localsearch.geneticalgorithm;

import playn.core.PlayN;

import com.googlecode.curvedmetromaps.core.model.MetroMap;

public class GeneticAlgorithm {
	private int populationSize = 4;
	private int generation = 0;
	
	private Population population;
	
	public GeneticAlgorithm(MetroMap initialMap) {
		population = new Population(initialMap, populationSize);
	}
	
	/**
	 * Creates a new generation.
	 * @return The best child map of the new generation.
	 */
	public MetroMap update() {
		double startTime = PlayN.currentTime();

		while (startTime + 200 > PlayN.currentTime()) {
			Mutation.parallelMutate(population);
			generation++;
	
			Selection.selection(population);
			
		}

		System.out.println(
				String.valueOf(generation) + "\t" +
				String.valueOf(population.organisms.get(0).cost) + "\t" +
				String.valueOf(population.organisms.get(0).mutationRate));
		
		return population.organisms.get(0).map;
	}
}
